﻿using System;
using System.Drawing;

namespace DarcRogue
{
    public enum Mode
    {
        Move,
        Run,
        Look,
        Cast,
        Use
    }
    public class Player
    {
        Map map;
        Hud hud;
        string name = "DEFAULT";
        Point pos;
        Point cursorPos = Point.Empty;
        Mode mode = Mode.Move;

        public Player(Map map, int x, int y)
        {
            this.map = map;
            pos = new Point(x, y);
        }

        public bool Update()
        {
            bool action = false;
            ConsoleKeyInfo? key = ReadKey();
            if (!key.HasValue)
                return action;
            while (true)
            {
                switch (mode)
                {
                    case Mode.Cast:
                        break;
                    case Mode.Look:
                        {
                            switch (key.Value.Key)
                            {
                                case ConsoleKey.LeftArrow:
                                    if (cursorPos.X > 0)
                                        cursorPos.X--;
                                    break;
                                case ConsoleKey.RightArrow:
                                    if (cursorPos.X < map.Width - 1)
                                        cursorPos.X++;
                                    break;
                                case ConsoleKey.UpArrow:
                                    if (cursorPos.Y > 0)
                                        cursorPos.Y--;
                                    break;
                                case ConsoleKey.DownArrow:
                                    if (cursorPos.Y < map.Height - 1)
                                        cursorPos.Y++;
                                    break;
                                case ConsoleKey.Escape:
                                    mode = Mode.Move;
                                    break;
                            }
                            hud.ClearMessages();
                            hud.AddMessage(map.GetMessage(cursorPos.X, cursorPos.Y));
                        }
                        break;
                    case Mode.Move:
                        {
                            switch (key.Value.Key)
                            {
                                case ConsoleKey.LeftArrow:
                                    if (!map.Solid(pos.X - 1, pos.Y))
                                        pos.X--;
                                    action = true;
                                    break;
                                case ConsoleKey.RightArrow:
                                    if (!map.Solid(pos.X + 1, pos.Y))
                                        pos.X++;
                                    action = true;
                                    break;
                                case ConsoleKey.UpArrow:
                                    if (!map.Solid(pos.X, pos.Y - 1))
                                        pos.Y--;
                                    action = true;
                                    break;
                                case ConsoleKey.DownArrow:
                                    if (!map.Solid(pos.X, pos.Y + 1))
                                        pos.Y++;
                                    action = true;
                                    break;
                                case ConsoleKey.L:
                                    mode = Mode.Look;
                                    cursorPos = pos;
                                    hud.ClearMessages();
                                    hud.AddMessage(map.GetMessage(cursorPos.X, cursorPos.Y));
                                    break;
                            }
                        }
                        break;
                    case Mode.Run:
                        break;
                    case Mode.Use:
                        break;
                }
                key = ReadKey();
                if (!key.HasValue)
                    return action;
            }
        }
        public void Draw()
        {
            Console.SetCursorPosition(pos.X, pos.Y);
            Console.Write("@");
            switch (mode)
            {
                case Mode.Look:
                    Console.SetCursorPosition(cursorPos.X, cursorPos.Y);
                    Console.Write("_");
                    break;
            }
        }

        ConsoleKeyInfo? ReadKey()
        {
            if (!Console.KeyAvailable)
                return null;
            return Console.ReadKey();
        }

        public Hud Hud { set { hud = value; } }

        public string Name
        {
            get
            {
                return name;
            }
            private set
            {
                name = value;
            }
        }
        public Point Position { get { return pos; } }
        public Map Map { get { return map; } set { map = value; } }
    }
}
